package compoundDB.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This class retains information about a compound within the CompoundDB system
 * such as GUID, OwnerID, names, and other data.
 */
public class Compound
{
	/**
	 * GUID is unique to the compound.
	 */
	private long m_guid;
	
	/**
	 * OwnerID identifies the owner of the compound.
	 */
	private long m_ownerid;
	
	/**
	 * A list of names that the compound has
	 */
	private List<String> m_names;
	
	/**
	 * A map containing the properties of the compound.
	 */
	private Map<String, Object> m_data;
	
	/**
	 * Creates a new compound with the specified GUID and Owner ID.
	 * 
	 * @param guid
	 *            The GUID of the compound. Should be set to zero if the
	 *            compound
	 * 
	 * @param ownerid
	 *            The id of the user who owns the compound. 0 is public.
	 */
	public Compound(long guid, long ownerid)
	{
		m_names = new ArrayList<String>();
		m_data = new HashMap<String, Object>();
		setGUID(guid);
		setOwnerID(ownerid);
	}
	
	/**
	 * Returns the Globally Unique IDentifier corresponding to the compound.
	 * 
	 * @return the GUID of the compound.
	 */
	public long getGUID()
	{
		return m_guid;
	}
	
	/**
	 * Sets the Globally Unique IDentifier corresponding to the compound.
	 * 
	 * @param guid the GUID of the compound.
	 */
	public void setGUID(long guid)
	{
		m_guid = guid;
		setProperty("guid", guid);
	}
	
	/**
	 * Returns the ID number corresponding to the owner of the compound.
	 * 
	 * @return the GUID of the compound.
	 */
	public long getOwnerID()
	{
		return m_ownerid;
	}
	
	/**
	 * Sets the Owner ID of to the compound.
	 * 
	 * @param ownerid the Owner ID of the compound.
	 */
	public void setOwnerID(long ownerid)
	{
		m_ownerid = ownerid;
		setProperty("ownerid", ownerid);
	}
	
	/**
	 * Looks up a specified property of the compound.
	 * 
	 * @param property
	 *            The property to retrieve.
	 * @return the retrieved property, or null if that property is not defined
	 *         for this compound.
	 */
	public Object getProperty(String property)
	{
		return m_data.get(property);
	}
	
	/**
	 * Sets a property of the compound.
	 * 
	 * @param propertyName
	 *            The name of the property to set.
	 * @param propertyValue
	 *            The new value of the property.
	 */
	public void setProperty(String propertyName, Object propertyValue)
	{
		if (propertyName.equals("guid"))
		{
			m_guid = (Long)propertyValue;
		}
		else if (propertyName.equals("ownerid"))
		{
			m_ownerid = (Long)propertyValue;
		}

		m_data.put(propertyName, propertyValue);
		
	}
	
	/**
	 * Gets the list of properties a compound has.
	 * 
	 * @return The list of properties for the compound.
	 */
	public Map<String, Object> getProperties()
	{
		return m_data;
	}
	
	/**
	 * Sets the list of properties a compound has.
	 * 
	 * @param properties The list of properties for the compound.
	 */
	public void setProperties(Map<String, Object> properties)
	{
		m_data = properties;
	}
	
	/**
	 * Returns a list of names that this compound is identified as.
	 * 
	 * @return A list of names for the compound.
	 */
	public List<String> getNames()
	{
		return m_names;
	}
	
	/**
	 * Sets the list of names that this compound is identified as.
	 * 
	 * @param names
	 *            A list of names for the compound.
	 */
	public void setNames(List<String> names)
	{
		m_names = names;
	}
	
	/**
	 * Adds a name to the list of names that this compound is identified as.
	 * 
	 * @param name
	 *            A name for this compound
	 */
	public void addName(String name) {
		m_names.add(name);
	}
	
	/**
	 * Removes a name from the list of names that this compound is identified as.
	 * 
	 * @param name
	 *            The name to remove
	 */
	public void removeName(String name) {
		m_names.remove(name);
	}
	
}
